﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microhard.WarehouseInsp.Buz.Interfaces;
using Microhard.Core.DBUtility;
using Microhard.WarehouseInsp.Buz.DataAccess;
using Microhard.WarehouseInsp.Buz.DTO;
using Microhard.Core;
using Microhard.WarehouseInsp.Buz.Msg;
using Microhard.WarehouseInsp.Buz.Models;
using Microhard.WarehouseInsp.Buz.BuzEnum;
using Microhard.WarehouseInsp.Buz.JHWorkflow;

namespace Microhard.WarehouseInsp.Buz.Impls
{
    public class PlgTimeRecordsImpl : IPlgTimeRecords
    {


        public Microhard.Core.DBUtility.ExecutedResult Add(PlgTimeRecordsModel model)
        {
            throw new NotImplementedException();
        }

        public Microhard.Core.DBUtility.ExecutedResult Update(PlgTimeRecordsModel model)
        {
            throw new NotImplementedException();
        }

        public Microhard.Core.DBUtility.ExecutedResult Delete(List<Guid> ids)
        {
            throw new NotImplementedException();
        }

        public Microhard.Core.DBUtility.ExecutedResult Delete(PlgTimeRecordsModel model)
        {
            throw new NotImplementedException();
        }

        public PlgTimeRecordsModel GetModelByID(Guid id)
        {
            throw new NotImplementedException();
        }

        public PlgTimeRecordsModel GetModelByModel(PlgTimeRecordsModel model)
        {
            throw new NotImplementedException();
        }

        public List<PlgTimeRecordsModel> Query(PlgTimeRecordsModel Model, int pageIndex, int pageSize, out int totalCount)
        {
            throw new NotImplementedException();
        }


        public List<PlgTimeRecordsModel> Query(PlgTimeRecordsModel Model)
        {
            List<PlgTimeRecordsModel> list = new List<PlgTimeRecordsModel>();

            WarehouseInspEntities db = new WarehouseInspEntities();
            //获取对应项目下的质押物并分组
            List<PledgePlanInRecord> proEntityes = db.PledgePlanInRecords.Where(p => p.ProjectID == Model.ProjectID).ToList();


            foreach (PledgePlanInRecord proentity in proEntityes)
            {
                List<OutInPutWarehouseRecord> outinEntitys = db.OutInPutWarehouseRecords.Where(p => p.PlnPdgID == proentity.PlnPdgID && p.PlayExTime >= Model.StartTime && p.PlayExTime <= Model.EndTime).ToList();

                foreach (OutInPutWarehouseRecord outinentity in outinEntitys)
                {
                    PlgTimeRecordsModel plg = new PlgTimeRecordsModel();
                    plg.PlnPdgID = proentity.PlnPdgID;
                    plg.PlgCategory = proentity.PlgCategory;
                    plg.CreateTime = outinentity.PlayExTime;

                    if (outinentity.OutInPutType == (int)OutInPutType.In)
                    {
                        plg.InCount = outinentity.OutInCount;
                        plg.OutCount = 0;
                    }
                    else
                    {
                        plg.InCount = 0;
                        plg.OutCount = outinentity.OutInCount;
                    }

                    PledgeChangeRecord changeRecord = db.PledgeChangeRecords.SingleOrDefault(p => p.OutInPutWarehouseRecordsID == outinentity.OutInPutWarehouseID);

                    plg.CurrentCount = changeRecord.CurrentCount;
                    plg.CurrentPrice = changeRecord.CurrentPrice;
                    plg.CurrentAmount = changeRecord.CurrentAmount;

                    list.Add(plg);
                }
                return list;
            }

            return new List<PlgTimeRecordsModel>();
        }

        public Microhard.Core.DBUtility.ScopeResult scope
        {
            set;
            get;
        }
    }
}
